package fr.labri.macchiato.framework.business.model.preferences;

/**
 * A time of the day, with the hours and the minutes
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public class TimeOfDay implements Comparable<TimeOfDay> {

	private int hour, minutes;

	/**
	 * Constructor with a string Format for the time is HHMM
	 * @param time : time to format 
	 */
	public TimeOfDay(String time) {
		hour = Integer.valueOf(time.substring(0,2));
		minutes = Integer.valueOf(time.substring(2, 4));

	}

	TimeOfDay() {
		hour = 0;
		minutes = 0;
	}

	/**
	 * @param hour
	 * @param minutes
	 */
	public TimeOfDay(int hour, int minutes) {
		this.hour = hour;
		this.minutes = minutes;
	}

	/**
	 * @return the hour
	 */
	public int getHour() {
		return hour;
	}

	/**
	 * @param hour
	 *            the hour to set
	 */
	public void setHour(int hour) {
		this.hour = hour;
	}

	/**
	 * @return the minutes
	 */
	public int getMinutes() {
		return minutes;
	}

	/**
	 * @param minutes
	 *            the minutes to set
	 */
	public void setMinutes(int minutes) {
		this.minutes = minutes;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TimeOfDay) {
			TimeOfDay tod = (TimeOfDay) obj;
			return (getHour() == tod.getHour() && getMinutes() == tod
					.getMinutes());
		}

		return false;
	}

	@Override
	public int compareTo(TimeOfDay o) {
		if (o.getHour() != getHour())
			return getHour() - o.getHour();
		return getMinutes() - o.getMinutes();
	}

	@Override
	public String toString() {
		String stringHour = "" , stringMinutes = "";
		if (getHour() < 10)
			stringHour = "0";
		stringHour += Integer.toString(this.getHour());
		
		if (getMinutes() < 10)
			stringMinutes = "0";
		stringMinutes += Integer.toString(this.getMinutes());

		return stringHour + stringMinutes;
	}
}
